/*
 * @Descripttion: 系统级别相关逻辑
 * @version: V1.0.0.1
 * @Author: zhaozhenzhuo
 * @Date: 2024-08-12 10:53:16
 * @LastEditors: zhaozhenzhuo
 * @LastEditTime: 2024-08-13 17:29:35
 */
import axios from 'axios';

// 校验接口是否是404
export const checkIf404WithPost = async (url: string): Promise<boolean> => {
	try {
		await axios.post(url);
		return false; // 请求成功，不是404
	} catch (error) {
		if (axios.isAxiosError(error) && error.response?.status === 404) {
			return true; // 是404错误
		}
		return false; // 其他错误或网络问题
	}
};

// 查询地址栏参数
export const getQuery = (variable: string): string | undefined => {
	const queryUrl = window.location.href.split('#')[1];
	const arr = queryUrl.split('?');
	const len = arr.length;
	const query = arr[len - 1];
	if (!query) {
		return undefined;
	}
	const vars = query.split('&');
	for (let i = 0; i < vars.length; i++) {
		const pair = vars[i].split('=');
		if (pair[0] === variable) {
			return pair[1];
		}
	}
	return undefined;
};

// 移除地址栏token
export const delTokenInUrl = (): void => {
	// eslint-disable-next-line no-useless-escape
	const reg = /(\?|\&)token=([A-Za-z0-9|\.|\-|\_]{1,9999})/gi;
	const url = window.location.href;
	const regResult = url.match(reg);
	console.log(regResult);
	if (regResult && regResult[0]) {
		let resUrl = url;
		for (let i = 0; i < regResult.length; i++) {
			resUrl = resUrl.replace(regResult[i], '');
		}
		if (!resUrl.includes('?')) {
			resUrl = resUrl.replace('&', '?');
		}
		console.log(resUrl);
		window.location.href = resUrl;
	}
};
